package org.yaac.client.widget.stat;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * @author Max Zhu (thebbsky@gmail.com)
 *
 */
public class CountSizeSingleFilterPanel extends FlowPanel {
	
	public static interface OnChangeHandler {
		void onChange(CountSizeFilterChoice val);
	}
	
	private RadioButton countRb;
	
	private RadioButton sizeRb;
	
	private List<OnChangeHandler> handlers;
	
	/**
	 * @param group
	 */
	public CountSizeSingleFilterPanel(String group) {
		handlers = new LinkedList<OnChangeHandler>();
		
		// count
		countRb = new RadioButton(group, "Count");	// TODO : i18n
		countRb.getElement().getStyle().setFloat(Style.Float.LEFT);
		countRb.setValue(true);	// default select count
		countRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(countRb);
		
		// size
		sizeRb = new RadioButton(group, "Size");	// TODO : i18n
		sizeRb.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				onChange();
			}
		});
		this.add(sizeRb);
	}
	
	/**
	 * @return
	 */
	public CountSizeFilterChoice selectedValue() {
		if (countRb.getValue()) {
			return CountSizeFilterChoice.COUNT;
		} else {
			return CountSizeFilterChoice.SIZE;
		}
	}
	
	public void addOnChangeHandler(OnChangeHandler handler) {
		this.handlers.add(handler);
	}
	
	private void onChange() {
		for (OnChangeHandler handler : handlers) {
			handler.onChange(selectedValue());
		}
	}
}
